package recordVod 
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.text.TextField;
	import recordVod.events.EventData;
	import recordVod.exchangeddata.UserInfo;
	import flash.display.LoaderInfo;
	import game.constants.ConstData;
	import flash.external.ExternalInterface;
	/**
	 * ...
	 * @author Bamboo
	 */
	public class RecordVod extends MovieClip
	{
		private	var	connectFMS		:ConnectFMS;
		private	var	userInfo		:UserInfo;
		public	var mcLoading		:MovieClip;
		private	var gameSet			:int;
		private	var	gameNo			:int;
		private var gameDate		:String = "";
		public  var testTf			:TextField;
		public function RecordVod() 
		{
			connectFMS = new ConnectFMS();
			userInfo = new UserInfo();
			this.connectFMS.addEventListener(EventData.SET_SESSIONTOKEN, connectRoomIdFMS);
			this.connectFMS.addEventListener(EventData.CONFIRMED_CONNECTION, confirmConnectHdl);
			this.connectFMS.addEventListener(EventData.START_GAME, startGameHdl);
			this.connectFMS.addEventListener(EventData.STOP_GAME, stopGameHdl);
			this.connectFMS.addEventListener(EventData.CLOSE_LOGIN, closeLoginHdl);
			this.connectFMS.addEventListener(EventData.SEND_STATUS_CONNECT, closeLoginHdl);
			this.connectFMS.addEventListener(EventData.SET_GAME_DATE, setGameDateHdl);
			//run app
			ExternalInterface.addCallback("Init_Vod", initVod);
			ExternalInterface.addCallback("writeLogConnect", updateText);
			//end run app
			//this.loaderInfo.addEventListener(Event.COMPLETE, connectGame);
			//end test
		}
		
		private function updateText(table, status, currentTime):void 
		{
			var temp:String = testTf.text;
			testTf.text = "table:" + table + " :status: " + status + " :currentTime: " + currentTime + "\n" + "========================================"  + "\n" + temp;
		}
		
		private function setGameDateHdl(e:EventData):void 
		{
			this.gameDate = e.result;
		}
		
		private function closeLoginHdl(e:EventData):void 
		{
			if(e.type== EventData.CLOSE_LOGIN){
				ExternalInterface.call("closeGame");	
				showLog("closeGame" + e.ms);
			}else if (e.type == EventData.SEND_STATUS_CONNECT) {
				showLog("SEND_STATUS_CONNECT:" + e.ms);
				ExternalInterface.call("receiveStatusConnect", e.ms);	
			}
		}
		
		private function connectGame(e:Event):void 
		{
			connectIntoFMS();
		}
		
		private function stopGameHdl(e:EventData):void 
		{
			trace("stopGameHdl" + e.result);
		}
		
		private function startGameHdl(e:EventData):void 
		{
			var result:String = e.result;
			if(result != "0" && this.gameDate != ""){
				this.gameSet = int(result.substr(ConstData.POS_BETSTART_GAMESET, ConstData.LEN_GAMESET));
				this.gameNo = int(result.substr(ConstData.POS_BETSTART_GAMENO, ConstData.LEN_GAMENO));	
				ExternalInterface.call("startGame", this.gameDate, this.gameSet, this.gameNo);
				showLog("startGame:" + e.result);
				this.gameDate = "";
			}
		}
		
		private function initVod(rtmp:String,versionNo:String, siteName:String, ncName:String, ip:String, countrycode:String,loginTypeId:String, parentSID:String, userName:String, userPass:String, roomId:String):void 
		{
			mcLoading.visible = false;
			GlobalVariables.rtmp = rtmp;
			GlobalVariables.versionNo = versionNo;
			GlobalVariables.siteName = siteName;
			GlobalVariables.ncName = ncName;
			GlobalVariables.ip = ip;
			GlobalVariables.countrycode = countrycode;
			GlobalVariables.loginTypeId = loginTypeId;
			GlobalVariables.parentSID = parentSID;
			GlobalVariables.user =  userName;
			GlobalVariables.userPass = userPass;
			GlobalVariables.roomId = roomId;
			loginGame();
		}
		
		private function loginGame():void 
		{
			connectIntoFMS();
			mcLoading.visible = true;
		}
		
		private function connectRoomIdFMS(e:EventData):void {	
			updateSessionToken(e.result);
			connectFMS.setRoomId = GlobalVariables.roomId
			connectFMS.setSessionId = userInfo.getSessionId();
			connectFMS.getConnectId();
		}
		
		private function updateSessionToken(result:String):void {
			userInfo.resultSetSessionToken(result);
			
		}
		
		private function confirmConnectHdl(e:EventData):void {
			mcLoading.visible = false;
		}	
		
		private function connectIntoFMS():void {
			
			connectFMS.setFmsPath = GlobalVariables.rtmp; // "110.173.55.49/NONSQL_KN";//"10.10.100.203/NONSQL_KN";//"localhost/Jewel2";//
			connectFMS.setRoomId = "0";
			connectFMS.setUserType = "MEMBER";
			connectFMS.setUserId = GlobalVariables.user; // "cacom01";
			connectFMS.setUserPassword = GlobalVariables.userPass; //"123456";
			connectFMS.setBetLtdSeq = "1";
			connectFMS.setSessionId = "0";
			connectFMS.setLanguage = GlobalVariables.lang; //E;
			connectFMS.setVersionNo = GlobalVariables.versionNo; //"1.0";
			connectFMS.setForceReload = "";
			connectFMS.setIsMain = "true";
			connectFMS.setSiteName = GlobalVariables.siteName; //"localhost";
			connectFMS.setCasinoId = "0";
			connectFMS.setNcName = GlobalVariables.ncName; // "localhost";
			connectFMS.setIp = GlobalVariables.ip; // "127.127.127.127";
			connectFMS.setCountryCode = GlobalVariables.countrycode; //"KH";
			connectFMS.setLoginTypeId = GlobalVariables.loginTypeId; // "3";
			connectFMS.setParentSID = GlobalVariables.parentSID; // "0";
			connectFMS.setKeno = "true";
			connectFMS.getConnect();
		}
		
		private function showLog(value:String):void {
			var time:Date = new Date();
			trace(value + " :time: " + time);
		}
	}

}